var user_role = sessionStorage.getItem('user_role')
var user_id = sessionStorage.getItem('user_id')
let project_div = document.getElementById('project_table')
let task_div = document.getElementById('task_table')
let my_task_div = document.getElementById('my_task_table')
// 显示界面
function showPage() {
  let user_manage = document.getElementById('user_manage')
  let project_manage = document.getElementById('project_manage')
  let money = document.getElementById('money')
  let contract = document.getElementById('contract')
  if (user_role == '组员') {
    user_manage.style.display = 'none'
    project_manage.style.display = 'none'
    money.style.display = 'none'
    contract.style.display = 'none'
  }
}
showPage()
//日期相减
Date.prototype.diff = function (date) {
  return (this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000)
}

taskTableInit = function () {
  data = {}
  data.user_id = user_id
  data.user_role = user_role
  data.action = 'get_member_progress'
  $.post('../project/action1', data, function (json) {
    json = JSON.parse(json)
    console.log(json)
    if (json.result === 1) {
      var list = json.aaData
      console.log(list)
      var html = ''
      if (list !== undefined && list.length > 0) {
        for (let i = 0; i < list.length; i++) {
          let record = list[i]
          console.log(record.startTime + record.dueTime)
          let startDate = new Date(record.startTime.replace(/-/g, '/'))
          let dueDate = new Date(record.dueTime.replace(/-/g, '/'))
          let leftDay = dueDate.diff(startDate)
          console.log(leftDay)
          if (leftDay < 0 && record.progress !== '100') {
            ///截止日期已经过了
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat blue-madison">' +
              '<div class="visual">' +
              '<i class="fa fa-comments"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '超时' +
              -leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '   成员：' +
              record.toWhoName +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay > 7 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat green-haze">' +
              '<div class="visual">' +
              '<i class="fa fa-shopping-cart"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '   成员：' +
              record.toWhoName +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay <= 7 && leftDay >= 0 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat red-intense">' +
              '<div class="visual">' +
              '<i class="fa fa-bar-chart-o"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '   成员：' +
              record.toWhoName +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
        }
      }
      $('#task_table_list').html(html)
    }
  })
}

myTaskTableInit = function () {
  data = {}
  data.user_id = user_id
  data.user_role = user_role
  data.action = 'get_task_info'
  $.post('../project/action1', data, function (json) {
    json = JSON.parse(json)
    console.log(json)
    if (json.result === 1) {
      var list = json.aaData
      console.log(list)
      var html = ''
      if (list !== undefined && list.length > 0) {
        for (let i = 0; i < list.length; i++) {
          let record = list[i]
          console.log(record.startTime + record.dueTime)
          let startDate = new Date(record.startTime.replace(/-/g, '/'))
          let dueDate = new Date(record.dueTime.replace(/-/g, '/'))
          let leftDay = dueDate.diff(startDate)
          console.log(leftDay)
          if (leftDay < 0 && record.progress !== '100') {
            ///截止日期已经过了
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat blue-madison">' +
              '<div class="visual">' +
              '<i class="fa fa-comments"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '超时' +
              -leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay > 7 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat green-haze">' +
              '<div class="visual">' +
              '<i class="fa fa-shopping-cart"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay <= 7 && leftDay >= 0 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat red-intense">' +
              '<div class="visual">' +
              '<i class="fa fa-bar-chart-o"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '任务:' +
              record.content +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
        }
      }
      $('#my_task_table_list').html(html)
    }
  })
}

projectTableInit = function () {
  data = {}
  data.user_id = user_id
  data.user_role = user_role
  data.action = 'get_project_info'
  $.post('../project/action', data, function (json) {
    json = JSON.parse(json)
    console.log(json)
    if (json.result_msg === 'ok') {
      var list = json.project_list
      console.log(list)
      var html = ''
      if (list !== undefined && list.length > 0) {
        for (let i = 0; i < list.length; i++) {
          let record = list[i]
          console.log(record.start_time + record.due_time)
          let startDate = new Date(record.start_time.replace(/-/g, '/'))
          let dueDate = new Date(record.due_time.replace(/-/g, '/'))
          let leftDay = dueDate.diff(startDate)
          console.log(leftDay)
          if (leftDay < 0 && record.progress !== '100') {
            ///截止日期已经过了
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat blue-madison">' +
              '<div class="visual">' +
              '<i class="fa fa-comments"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '超时' +
              -leftDay +
              '天</div>' +
              '<div class="desc">' +
              '项目:' +
              record.project_name +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay > 7 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat green-haze">' +
              '<div class="visual">' +
              '<i class="fa fa-shopping-cart"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '项目:' +
              record.project_name +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
          if (leftDay <= 7 && leftDay >= 0 && record.progress !== '100') {
            ///剩余时间充足
            html =
              html +
              '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">' +
              '<div class="dashboard-stat red-intense">' +
              '<div class="visual">' +
              '<i class="fa fa-bar-chart-o"></i>' +
              '</div>' +
              '<div class="details">' +
              '<div class="number">' +
              '剩余' +
              leftDay +
              '天</div>' +
              '<div class="desc">' +
              '项目:' +
              record.project_name +
              '</div>' +
              '</div>' +
              '<a class="more" href="query.html">' +
              '详情 <i class="m-icon-swapright m-icon-white"></i>' +
              '</a>' +
              '</div>' +
              '</div>'
          }
        }
      }
      $('#project_table_list').html(html)
    }
  })
}

if (user_role === '管理员') {
  task_div.style.display = 'none'
  my_task_div.style.display = 'none'
  projectTableInit()
}
if (user_role === '组长') {
  my_task_div.style.display = 'none'
  projectTableInit()
  taskTableInit()
}
if (user_role === '组员') {
  project_div.style.display = 'none'
  task_div.style.display = 'none'
  myTaskTableInit()
}
